<template>
  <el-form label-width="100px">
    <el-form-item label="SPU名称">
      <el-input
        placeholder="请你输入SPU名称"
        v-model="SpuParams.spuName"
      ></el-input>
    </el-form-item>
    <el-form-item label="SPU品牌">
      <el-select v-model="SpuParams.tmId">
        <el-option
          v-for="(item, index) in AllTradeMark"
          :key="item.id"
          :label="item.tmName"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="SPU描述">
      <el-input
        type="textarea"
        placeholder="请你输入SPU描述"
        v-model="SpuParams.description"
      ></el-input>
    </el-form-item>
    <el-form-item label="SPU图片">
      <!-- v-model:fileList->展示默认图片 
                 action:上传图片的接口地址
                 list-type:文件列表的类型
            -->
      <el-upload
        v-model:file-list="imgList"
        action=""
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
        :http-request="handleAvatarSuccess"
      >
        <el-icon>
          <Plus />
        </el-icon>
      </el-upload>
      <el-dialog v-model="dialogVisible">
        <img
          w-full
          :src="dialogImageUrl"
          alt="Preview Image"
          style="width: 100%; height: 100%"
        />
      </el-dialog>
    </el-form-item>
    <el-form-item label="SPU销售属性">
      <!-- 展示销售属性的下拉菜单 -->
      <el-select
        v-model="saleAttrIdAndValueName"
        :placeholder="
          unSelectSaleAttr.length
            ? `还未选择${unSelectSaleAttr.length}个`
            : '无'
        "
      >
        <el-option
          :value="`${item.id}:${item.name}`"
          v-for="(item, index) in unSelectSaleAttr"
          :key="item.id"
          :label="item.name"
        ></el-option>
      </el-select>
      <el-button
        @click="addSaleAttr"
        :disabled="saleAttrIdAndValueName ? false : true"
        style="margin-left: 10px"
        type="primary"
        size="default"
        icon="Plus"
        >添加属性</el-button
      >
      <!-- table展示销售属性与属性值的地方 -->
      <el-table border style="margin: 10px 0px" :data="saleAttr">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="销售属性名字"
          width="120px"
          prop="saleAttrName"
        ></el-table-column>
        <el-table-column label="销售属性值">
          <!-- row:即为当前SPU已有的销售属性对象 -->
          <template #="{ row, $index }">
            <el-tag
              style="margin: 0px 5px"
              @close="row.spuSaleAttrValueList.splice(index, 1)"
              v-for="(item, index) in row.spuSaleAttrValueList"
              :key="row.id"
              class="mx-1"
              closable
            >
              {{ item.saleAttrValueName }}
            </el-tag>
            <el-input
              @blur="toLook(row)"
              v-model="row.saleAttrValue"
              v-if="row.flag == true"
              placeholder="请你输入属性值"
              size="small"
              style="width: 100px"
            ></el-input>
            <el-button
              @click="toEdit(row)"
              v-else
              type="primary"
              size="small"
              icon="Plus"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Delete"
              @click="saleAttr.splice($index, 1)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button
        :disabled="saleAttr.length > 0 ? false : true"
        type="primary"
        size="default"
        @click="save"
        >保存</el-button
      >
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, computed, defineExpose, tor } from "vue";
import { ElMessage } from "element-plus";
import {
  reqUploadImgs,
  reqAllTradeMark, //所有品牌
  reqAllSaleAttr, //全部的销售属性
  reqSpuImageList, //获取详情图片
  reqSpuHasSaleAttr, //某一个sku下的详情销售属性
  reqAddOrUpdateSpu, //添加修改
} from "@/api/spu/index";
import {
  Trademark,
  AllTradeMark,
  SpuImg,
  SpuHasImg,
  SaleAttr,
  HasSaleAttr,
  spuSaleAttResponseData,
  HasSaleAttrResponseData,
} from "@/api/spu/type";
let $emit = defineEmits(["changeScene"]);
//点击取消按钮:通知父组件切换场景为1,展示有的SPU的数据
const cancel = () => {
  $emit("changeScene", { flag: 0, params: "update" });
};
//存储已有的SPU这些数据
let AllTradeMark = ref<Trademark[]>([]);
//商品图片
let imgList = ref<SpuImg[]>([]);
//已有的SPU销售属性
let saleAttr = ref<SaleAttr[]>([]);
//全部销售属性
let allSaleAttr = ref<HasSaleAttr[]>([]);
//控制对话框的显示与隐藏
let dialogVisible = ref<boolean>(false);
//存储预览图片地址
let dialogImageUrl = ref<string>("");
//存储已有的SPU对象
let SpuParams = ref<any>({
  category3Id: "", //收集三级分类的ID
  spuName: "", //SPU的名字
  description: "", //SPU的描述
  tmId: "", //品牌的ID
  spuImageList: [],
  spuSaleAttrList: [],
});
//将来收集还未选择的销售属性的ID与属性值的名字
let saleAttrIdAndValueName = ref<string>("");
//子组件书写一个方法
const initHasSpuData = async (spu: any) => {
  console.log("父组件船只", spu);
  SpuParams.value = spu;
  const resultImg: SpuHasImg = await reqSpuImageList(spu.id as number);
  // 获取全部品牌的数据
  //获取全部品牌的数据
  let AllTrade: AllTradeMark = await reqAllTradeMark();
  let result1: HasSaleAttrResponseData = await reqAllSaleAttr();
  const resultAttr: spuSaleAttResponseData = await reqSpuHasSaleAttr(
    spu.id as number
  );
  imgList.value = resultImg.data.map((item: SpuImg) => ({
    ...item,
    name: item.imgName,
    url: item.imgUrl,
  }));
  saleAttr.value = resultAttr.data;

  //存储已有的SPU的销售属性
  AllTradeMark.value = AllTrade.data;
  //存储全部的销售属性
  allSaleAttr.value = result1.data;
};
// 图片上传
const handleAvatarSuccess = async (e: any) => {
  console.log(e);

  // 定义结构参数的类型
  interface fileType {
    size: number;
    type: string;
  }
  const { size, type }: fileType = e.file;
  if (istrue(size, type)) {
    let params: any = new FormData();
    params.append("file", e.file);
    const result: any = await reqUploadImgs(params);
    if (result.code == 200) {
      //  trademarkParams.logoUrl=result.data;
      ElMessage({
        type: "success",
        message: result.message,
      });
    }
  }
};
// 上传验证
const istrue = (size: number, type: string): boolean => {
  if (
    type == "image/png" ||
    type == "image/jpeg" ||
    type == "image/jpg" ||
    type == "image/gif"
  ) {
    if (size / 1024 / 1024 < 4) {
      return true;
    } else {
      ElMessage({
        type: "error",
        message: "上传文件大小小于4M",
      });
      return false;
    }
  } else {
    ElMessage({
      type: "error",
      message: "上传文件格式务必PNG|JPG|GIF",
    });
    return false;
  }
};
//照片墙删除文件钩子
const handleRemove = () => {
  console.log(111);
};
//照片墙点击预览按钮的时候触发的钩子
const handlePictureCardPreview = (file: any) => {
  dialogImageUrl.value = file.url;
  //对话框弹出来
  dialogVisible.value = true;
};
//计算出当前SPU还未拥有的销售属性
let unSelectSaleAttr = computed(() => {
  //全部销售属性:颜色、版本、尺码
  //已有的销售属性:颜色、版本
  let unSelectArr = allSaleAttr.value.filter((item) => {
    return saleAttr.value.every((item1) => {
      return item.name != item1.saleAttrName;
    });
  });
  return unSelectArr;
});

//添加销售属性的方法
const addSaleAttr = () => {
  console.log(saleAttrIdAndValueName, "saleAttrIdAndValueName");
  /*
    "baseSaleAttrId": number,
    "saleAttrName": string,
    "spuSaleAttrValueList": SpuSaleAttrValueList
    */
  const [baseSaleAttrId, saleAttrName] =
    saleAttrIdAndValueName.value.split(":");
  //准备一个新的销售属性对象:将来带给服务器即可
  let newSaleAttr: any = {
    baseSaleAttrId,
    saleAttrName,
    spuSaleAttrValueList: [],
  };
  //追加到数组当中
  saleAttr.value.push(newSaleAttr);
  //清空收集的数据
  saleAttrIdAndValueName.value = "";
};

//属性值按钮的点击事件
const toEdit = (row: any) => {
  row.flag = true;
  row.saleAttrValue = "";
};
//表单元素失却焦点的事件回调
const toLook = (row: any) => {
  console.log(row);
  const { baseSaleAttrId, saleAttrValue } = row;

  if ((saleAttrValue as string).trim() == "") {
    ElMessage({
      type: "error",
      message: "属性值不能为空的",
    });
    return;
  }
  let repeat = row.spuSaleAttrValueList.find((item) => {
    return item.saleAttrValueName == saleAttrValue;
  });

  if (repeat) {
    ElMessage({
      type: "error",
      message: "属性值重复",
    });
    return;
  }
  let newSaleAttrValue: any = {
    baseSaleAttrId,
    saleAttrValueName: saleAttrValue,
  };

  //追加新的属性值对象
  row.spuSaleAttrValueList.push(newSaleAttrValue);
  //切换为查看模式
  row.flag = false;
};

//保存按钮的回调
const save = async () => {
  SpuParams.value.spuImageList = imgList.value.map((item: any) => {
    return {
      imgName: item.name, //图片的名字
      imgUrl: (item.response && item.response.data) || item.url,
    };
  });
  //2:整理销售属性的数据
  SpuParams.value.spuSaleAttrList = saleAttr.value;
  let result = await reqAddOrUpdateSpu(SpuParams.value);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: SpuParams.value.id ? "更新成功" : "添加成功",
    });
    //通知父组件切换场景为0
    $emit("changeScene", {
      flag: 0,
      params: SpuParams.value.id ? "update" : "add",
    });
  } else {
    ElMessage({
      type: "success",
      message: SpuParams.value.id ? "更新成功" : "添加成功",
    });
  }
};

//添加一个新的SPU初始化请求方法
const initAddSpu = async (c3Id: number | string) => {
  //清空数据
  Object.assign(SpuParams.value, {
    category3Id: "", //收集三级分类的ID
    spuName: "", //SPU的名字
    description: "", //SPU的描述
    tmId: "", //品牌的ID
    spuImageList: [],
    spuSaleAttrList: [],
  });
  console.log(SpuParams.value);

  //清空照片
  imgList.value = [];
  //清空销售属性
  saleAttr.value = [];
  saleAttrIdAndValueName.value = "";
  //存储三级分类的ID
  SpuParams.value.category3Id = c3Id;
  // 获取全部品牌的数据
  //获取全部品牌的数据
  let result: AllTradeMark = await reqAllTradeMark();
  let result1: HasSaleAttrResponseData = await reqAllSaleAttr();

  //存储数据
  AllTradeMark.value = result.data;
  allSaleAttr.value = result1.data;
};
//对外暴露
defineExpose({ initHasSpuData, initAddSpu });
</script>

<style scoped></style>
